package indi.kider.server.woniubus.api;

import indi.kider.common.model.apabus.Relet;
import indi.kider.common.model.apabus.Rent;
import indi.kider.common.model.apartment.Houses;
import indi.kider.common.model.employee.Employee;
import indi.kider.common.model.employee.Tenant;
import indi.kider.common.model.woniubus.HouseVo;
import indi.kider.common.model.woniubus.TenantUpdateVo;
import indi.kider.common.model.woniubus.TenantVo;
import indi.kider.server.woniubus.service.WoniuBusService;
import indi.kider.server.woniubus.service.related.AuthcService;
import indi.kider.server.woniubus.service.related.BillService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import pers.vankid.common.model.base.CommonResponse;
import pers.vankid.common.utils.page.PageRequest;
import pers.vankid.common.utils.page.PageResponse;
import pers.vankid.common.utils.page.PageUtil;

import java.util.List;
import java.util.Map;

/**
 * rent + tenant
 * 二期业务接口
 */
@RestController
@RequestMapping(value = "wnbus")
public class WnBusController {

    @Autowired
    AuthcService authcService;
    @Autowired
    WoniuBusService woniuBusService;
    @Autowired
    private BillService billService;

    /**
     * @api {POST} rentHouse
     * @apiGroup wnbus
     * @apiDescription 用户租房
     * @apiParamExample 请求样例：
     * http://localhost:9002/woniubus/wnbus/rentHouse
     * {"rentDate":"2018-05-06","expireDate":"2019-05-06","housesId":"82",""name":"wbb","phone":"13333333333","cardId":"44","remark":"44",priceSet:null,}
     * @apiSuccessExample {json} 返回样例:
     *
     * {
     * "status": "201",
     * "msg": "保存成功！",
     * "data": 1
     * }
     */
    @PostMapping("/rentHouse")
    public CommonResponse rentHouse(@RequestBody TenantVo tenantVo) {
        try {
            Employee employee = authcService.getCurrentUser().getData();
            tenantVo.setOpInId(employee.getId());
            CommonResponse  c =  woniuBusService.rentHouse(tenantVo);
            //调用 账单接口=======创建钱包==================
            try{
                if(c.getData()!=null){
                    Tenant t = (Tenant)c.getData();
                    billService.regitWallet(t.getId());
                }
            }catch (Exception e){
                e.printStackTrace();
            }
            return c;
        } catch (Exception e) {
            return CommonResponse.buildExceptionResponse(e);
        }
    }

    /**
     * @api {POST} /reletHouse
     * @apiGroup wnbus
     * @apiDescription 用户续租
     * @apiParamExample 请求样例：
     * http://localhost:9002/woniubus/wnbus/reletHouse
     * {"newExpireDate":"2019-05-06","rentId":"82"}
     * @apiSuccessExample {json} 返回样例:
     * {"status": "202","msg": "保存成功！","data": ""}
     */
    @PutMapping("/reletHouse")
    public CommonResponse<Rent> reletHouse(@RequestBody Relet relet) {
        try {
            Employee employee = authcService.getCurrentUser().getData();
            relet.setOpId(employee.getId());
            return woniuBusService.reletHouse(relet);
        } catch (Exception e) {
            return CommonResponse.buildExceptionResponse(e);
        }
    }
    /**
     * @api {POST} /reletHouse
     * @apiGroup wnbus
     * @apiDescription 用户退租
     * @apiParamExample 请求样例：
     * http://localhost:9002/woniubus/wnbus/rentOutHouse
     * {"rentId":"82"}
     * @apiSuccessExample {json} 返回样例:
     * {"status": "202","msg": "保存成功！","data": ""}
     */
    @PutMapping("/rentOutHouse")
    public CommonResponse<Rent> rentOutHouse(@RequestBody Rent rent) {
        try {
            Employee employee = authcService.getCurrentUser().getData();
            rent.setOpOutId(employee.getId());
            CommonResponse  c = woniuBusService.rentOutHouse(rent);
            //调用 账单接口=======扣钱==================
            try{
                if(rent.getId()!=null){
                    billService.deductionBill(rent.getId());
                }
            }catch (Exception e){
                e.printStackTrace();
            }
            return c;
        } catch (Exception e) {
            return CommonResponse.buildExceptionResponse(e);
        }
    }

    /**
     * @api {POST} getTenantListByHouseId
     * @apiGroup wnbus
     * @apiDescription 根据房间ID查询租客列表（分页）
     * @apiParamExample 请求样例：
     * http://localhost:9002/woniubus/wnbus/getTenantListByHouseId
     * {"num":"0","size":"8","query":{housesId:1}}
     * @apiSuccessExample {json} 返回样例:
     * {"status":"200","msg":"查询成功！","data":{"rows":[{"rentDate":"2018-05-15","expireDate":"2018-05-15","id":76,"name":"shixj1","cardId":"12212","phone":"1221","title":null,"nationality":"32","professional":"23","wxopenId":null,"state":1,"type":2,"friendsSize":null,"mainId":69}],"total":1,"size":0}}
     */
    @PostMapping("getTenantListByHouseId")
    public CommonResponse<PageResponse<List<Map>>> getTenantListByHouseId(@RequestBody PageRequest<Rent> request) {
        return CommonResponse.buildGetSuccessResponse(PageUtil.getPageResponse(woniuBusService.getTenantListByHouseId(request)));
    }

    /**
     * @api {POST} getMovedTenantListByHouseId
     * @apiGroup wnbus
     * @apiDescription 根据房间ID查询租客列表【已搬离】（分页）
     * @apiParamExample 请求样例：
     * http://localhost:9002/woniubus/wnbus/getMovedTenantListByHouseId
     * {"num":"0","size":"8","query":{housesId:1}}
     * @apiSuccessExample {json} 返回样例:
     * {"status":"200","msg":"查询成功！","data":{"rows":[{"rentDate":"2018-05-15","expireDate":"2018-05-15","id":76,"name":"shixj1","cardId":"12212","phone":"1221","title":null,"nationality":"32","professional":"23","wxopenId":null,"state":1,"type":2,"friendsSize":null,"mainId":69}],"total":1,"size":0}}
     */
    @PostMapping("getMovedTenantListByHouseId")
    public CommonResponse<PageResponse<List<Map>>> getMovedTenantListByHouseId(@RequestBody PageRequest<Rent> request) {
        return CommonResponse.buildGetSuccessResponse(PageUtil.getPageResponse(woniuBusService.getMovedTenantListByHouseId(request)));
    }

    /**
     * 不分页
     * @param {housesId:1}
     * http://localhost:9002/woniubus/wnbus/getTenantListByHouseIdNoPage
     * @return
     */
    @PostMapping("/getTenantListByHouseIdNoPage")
    public CommonResponse getTenantListByHouseIdNoPage(@RequestBody Rent rent) {
        return CommonResponse.buildGetSuccessResponse(woniuBusService.getTenantListByHouseIdNoPage(rent));
    }
    /**
     * @api {PUT} modifyTenantSex
     * @apiGroup wnbus
     * @apiDescription 修改租客性别
     * @apiParamExample 请求样例：
     * http://localhost:9002/woniubus/wnbus//modifyTenantSex
     * {rentId:1,id:1,sex:1}
     * @apiSuccessExample {json} 返回样例:
     * {"status":"202","msg":"修改性别成功!"}
     */
    @PutMapping("modifyTenantSex")
    public CommonResponse modifyTenantSex(@RequestBody TenantUpdateVo tenantUpdateVo) {
        try {
            return woniuBusService.modifyTenantSex(tenantUpdateVo);
        } catch (Exception e) {
            return CommonResponse.buildExceptionResponse(e);
        }
    }

    /**
     * @api {PUT} /modifyTenant
     * @apiGroup wnbus
     * @apiDescription 修改租客信息
     * @apiParamExample 请求样例：
     * http://localhost:9002/woniubus/wnbus/modifyTenant
     * {rentId:1 ... 客户信息..}
     * @apiSuccessExample {json} 返回样例:
     * {"status":"202","msg":"修改用户信息成功!"}
     */
    @PutMapping("modifyTenant")
    public CommonResponse modifyTenant(@RequestBody TenantUpdateVo tenantUpdateVo) {
        try {
            return woniuBusService.modifyTenant(tenantUpdateVo);
        } catch (Exception e) {
            return CommonResponse.buildExceptionResponse(e);
        }
    }


    /**
     * 根据身份证查租客信息
     * @param cardId
     * @return
     */
    @GetMapping("/getTenantByCardId/{cardId}")
    public CommonResponse<Tenant> getTenantByCardId(@PathVariable String cardId) {
        try {
            return CommonResponse.buildGetSuccessResponse(woniuBusService.getTenantByCardId(cardId));
        } catch (Exception e) {
            return CommonResponse.buildExceptionResponse(e);
        }
    }
    /**
     * 根据身份证查一体机信息
     * @param cardId
     * @return
     */
    @GetMapping("/getDevTenantByCardId/{cardId}")
    public CommonResponse<Tenant> getDevTenantByCardId(@PathVariable String cardId) {
        try {
            return CommonResponse.buildGetSuccessResponse(woniuBusService.getDevTenantByCardId(cardId));
        } catch (Exception e) {
            return CommonResponse.buildExceptionResponse(e);
        }
    }

    /**
     * 修改发布状态
     * @param houses
     * @return
     */
    @PostMapping("/updateHouseRelease")
    public CommonResponse updateHouseRelease(@RequestBody Houses houses) {
        try {
            return  woniuBusService.updateHouseRelease(houses);
        } catch (Exception e) {
            return CommonResponse.buildExceptionResponse(e);
        }
    }
}
